package org.xxx.epms.admin.models;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

public class AdminDetails implements UserDetails {

    private String adminCode;
    private String adminPassword;
    private List<AdminPermission> adminResourcesList;

    public AdminDetails(Admin admin, List<AdminPermission> adminResourcesList) {
        this.adminCode = admin.getAdminCode();
        this.adminPassword = admin.getAdminPassword();
        this.adminResourcesList = adminResourcesList;
    }

    public List<AdminPermission> getAdminResourcesList() {
        return adminResourcesList;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        //返回当前用户的角色
        return adminResourcesList.stream()
                .map(permission ->new SimpleGrantedAuthority("[Admin-Permission]" + permission.getAdminPermissionName() + "(" +  permission.getAdminPermissionId() + ")" ))
                .collect(Collectors.toList());
    }

    @Override
    public String getPassword() {return this.adminPassword;
    }

    @Override
    public String getUsername() {
        return this.adminCode;
    }

    @Override
    public boolean isAccountNonExpired() {
        return false;
    }

    @Override
    public boolean isAccountNonLocked() {
        return false;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return false;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
